<template>
  <div class="pay-success-container">
    <div v-if="payStatus === 1" class="success-content">
      <div class="success-icon">
        <i class="el-icon-success"></i>
      </div>
      <h2 class="success-title">支付成功</h2>
      <div class="order-info">
        <p>订单编号：{{ paymentInfo.orderNo }}</p>
        <p>支付金额：¥{{ paymentInfo.payAmount }}</p>
        <p>支付时间：{{ paymentInfo.payTime }}</p>
      </div>
      <div class="actions">
        <el-button type="primary" @click="goToHome">返回首页</el-button>
        <el-button @click="viewOrder">查看订单</el-button>
      </div>
    </div>
    <div v-else class="error-content">
      <div class="error-icon">😢</div>
      <h2 class="error-title">订单丢失了~~~</h2>
      <div class="actions">
        <el-button type="primary" @click="goToHome">返回首页</el-button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import payApi, { PaymentResponse } from '@/api/pay'

const route = useRoute()
const router = useRouter()

const payStatus = ref<number>(0)
const paymentInfo = ref<PaymentResponse>({
  orderNo: '',
  name: '',
  phone: '',
  address: '',
  remark: '',
  totalAmount: 0,
  discountAmount: 0,
  payAmount: 0,
  paymentMethod: 1,
  payStatus: 0,
  payTime: '',
  products: []
})

const orderNo = (route.query.out_trade_no || route.query.orderNo) as string

onMounted(() => {
  if (orderNo) {
    checkPaymentStatus(orderNo)
  } else {
    ElMessage.error('缺少订单编号')
    router.push('/')
  }
})

const checkPaymentStatus = async (orderNo: string) => {
  try {
    const res = await payApi.queryPayment(orderNo)
    paymentInfo.value = res.data.data
    payStatus.value = res.data.data.payStatus
  } catch (error) {
    console.error('查询支付结果失败:', error)
    ElMessage.error('查询支付结果失败')
  }
}

const goToHome = () => {
  router.push('/')
}

const viewOrder = () => {
  router.push(`/payment?orderNo=${orderNo}&show=true`)
}
</script>

<style scoped>
.pay-success-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.success-content, .error-content {
  text-align: center;
  background: white;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  max-width: 500px;
  width: 100%;
}

.success-icon {
  font-size: 64px;
  color: #67c23a;
  margin-bottom: 20px;
}

.success-title {
  color: #333;
  margin-bottom: 30px;
  font-size: 24px;
}

.order-info {
  text-align: left;
  background: #f9f9f9;
  padding: 20px;
  border-radius: 4px;
  margin-bottom: 30px;
}

.order-info p {
  margin: 10px 0;
  color: #666;
}

.error-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.error-title {
  color: #333;
  margin-bottom: 30px;
  font-size: 24px;
}

.actions {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

.actions .el-button {
  min-width: 120px;
}
</style>